<?php
/**
 * Created by PhpStorm.
 * User: hui
 * Date: 2017/2/26
 * Time: 13:45
 */
namespace app\admin\logic;

use think\Model;

class Module extends Model
{
    /**
     * 获取角色列表
     * by kuddy
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getRoles()
    {
        return db('admin_role')->select();
    }

    /**
     * 获取模块
     * by kuddy
     * @param string $id
     * @return array|false|\PDOStatement|string|Model
     */
    public function getModule($id = '')
    {
        $db = db('admin_module');
        if (empty($id)) {
            $res = $db->select();
            $list = $this->getTree($res);
        } else {
            $list = $db->where('module_id', $id)->find();
        }
        return $list;
    }

    /**
     * 模块列表树化
     * by kuddy
     * @param     $data
     * @param int $pid
     * @return array
     */
    public function getTree($data, $pid = 0)
    {
        $res = [];
        foreach ($data as $item) {
            if ($item['parent_id'] == $pid) {
                $item['child'] = $this->getTree($data, $item['module_id']);
                $res[] = $item;
            }
        }
        return $res;
    }

    /**
     * 获取菜单
     * by kuddy
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getMenu()
    {
        return db('admin_module')->where('type', 'top')->select();
    }

    public function setModule()
    {
        $post = input('post.');
        $map = [
            'module_id' => $post['module_id']
        ];
        return db('admin_module')
            ->where($map)
            ->update($post);
    }
}